/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"
#include "sys/time.h"

//#define _TIME_2_
//#define _TIME_3_
struct timezone tz;

extern const Ipp8u _nIZigZagIndex[64];

/******************************************************************************
// Name:            ijDecodeScanData_Baseline
// Description:     Decoding scan data for Baseline mode
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                     
//                  
******************************************************************************/
IJXRESULT ijDecodeScanData_Baseline( IJBITTREAMSTATE  *pStream,
                                     IJDECODESTATE    *pDecodeState)
{
	printf("ijDecodeScanData_BaseLine\n");
    int x, y;

    int nHMCUPos, nVMCUPos;
    int nHMCUCount, nVMCUCount;
    int nMCUWidth, nMCUHeight;
    int nMCUPadWidth, nMCUPadHeight;
    int nImageMCUHOffset, nImageMCUVOffset;

    int nTempImageStep;
    int nProcessedBitsLen;
    Ipp16s **ppCoeffBuf;
    Ipp16s **ppMCUBuf;
    Ipp8u  *pTempImageData;
    Ipp8u  *pImageCur;

    Ipp8u *pImageData;
    int   nImageStep;

    IppiSize dstSize;

    IJBUFFER *pIJXBuffer;
    IJMCUSPEC *pMCUSpec;
    IIPIMAGESPEC *pImageSpec;
    IJDEPROCESSSPEC *pProcSpec;

    pIJXBuffer=&(pDecodeState->bufSpec);
    pMCUSpec=&(pDecodeState->mcuSpec);
    pImageSpec=&(pDecodeState->imageSpec);
    pProcSpec=&(pDecodeState->procSpec);

    pImageData=pIJXBuffer->paImageBufCur[0];
    nImageStep=pIJXBuffer->naImageStep[0];

    nHMCUPos=pDecodeState->nHMCUCurPos;
    nVMCUPos=pDecodeState->nVMCUCurPos;
    nHMCUCount=pMCUSpec->nHMCUCount;
    nVMCUCount=pMCUSpec->nVMCUCount;
    nMCUWidth=pMCUSpec->nMCUWidth;
    nMCUHeight=pMCUSpec->nMCUHeight;
    nMCUPadWidth=pMCUSpec->nMCUPadWidth;
    nMCUPadHeight=pMCUSpec->nMCUPadHeight;

    nImageMCUHOffset=((pImageSpec->nBitsPerpixel)>>3)*nMCUWidth;
    nImageMCUVOffset=nImageStep*nMCUHeight;

    ppCoeffBuf=(Ipp16s **)(pIJXBuffer->paImgCoeffBufCur);
    ppMCUBuf =(Ipp16s **)(pIJXBuffer->paMCUCoeffBufCur);

    nTempImageStep=nImageMCUHOffset;
    pTempImageData=pIJXBuffer->pAuxMCUDataTempBuf;

    nProcessedBitsLen=pStream->nUsedBufBitsLen;
    
			gettimeofday(&tv_vld0,&tz);
    for (y=nVMCUPos; y<nVMCUCount; y++) {
        pImageCur=pImageData;
        for (x=nHMCUPos; x<nHMCUCount; x++) {

#ifdef _GET_M_P
			gettimeofday(&tv_vld0,&tz);
#endif
            pProcSpec->funcHuffmanDecoder( (const Ipp16s **)ppCoeffBuf, pStream, pDecodeState);
#ifdef _GET_M_P
			gettimeofday(&tv_vld1,&tz);
			tv_vld.tv_sec += tv_vld1.tv_sec - tv_vld0.tv_sec;
			tv_vld.tv_usec += tv_vld1.tv_usec - tv_vld0.tv_usec;

			gettimeofday(&tv_iqit0,&tz);
#endif
#ifdef _TIME_2_
            pProcSpec->funcMCUTransform((const Ipp16s **)ppCoeffBuf, ppMCUBuf,
                                        (const Ipp32s **)(pDecodeState->paIdxDCTDequantTable));
#endif
#ifdef _GET_M_P
			gettimeofday(&tv_iqit1,&tz);
			tv_iqit.tv_sec += tv_iqit1.tv_sec - tv_iqit0.tv_sec;
			tv_iqit.tv_usec += tv_iqit1.tv_usec - tv_iqit0.tv_usec;

			gettimeofday(&tv_cc0,&tz);
#endif
#ifdef _TIME_3_
            pProcSpec->funcMCUColorConvert((const Ipp16s **)ppMCUBuf, pImageCur, nImageStep);
#endif
#ifdef _GET_M_P
			gettimeofday(&tv_cc1,&tz);
			tv_cc.tv_sec += tv_cc1.tv_sec - tv_cc0.tv_sec;
			tv_cc.tv_usec += tv_cc1.tv_usec - tv_cc0.tv_usec;
#endif

            pImageCur+=nImageMCUHOffset;
        } //for (x=nHMCUPos; x<nHMCUCount; x++) 

        if (0<nMCUPadWidth) {
            dstSize.width=nMCUPadWidth;
            dstSize.height=nMCUHeight;

#ifdef _GET_M_P
			gettimeofday(&tv_vld0,&tz);
#endif
            pProcSpec->funcHuffmanDecoder((const Ipp16s **)ppCoeffBuf, pStream, pDecodeState);
#ifdef _GET_M_P
			gettimeofday(&tv_vld1,&tz);
			tv_vld.tv_sec += tv_vld1.tv_sec - tv_vld0.tv_sec;
			tv_vld.tv_usec += tv_vld1.tv_usec - tv_vld0.tv_usec;

			gettimeofday(&tv_iqit0,&tz);
#endif
#ifdef _TIME_2_
            pProcSpec->funcMCUTransform((const Ipp16s **)ppCoeffBuf, ppMCUBuf, 
                                        (const Ipp32s **)(pDecodeState->paIdxDCTDequantTable));
#endif
#ifdef _GET_M_P
			gettimeofday(&tv_iqit1,&tz);
			tv_iqit.tv_sec += tv_iqit1.tv_sec - tv_iqit0.tv_sec;
			tv_iqit.tv_usec += tv_iqit1.tv_usec - tv_iqit0.tv_usec;

			gettimeofday(&tv_cc0,&tz);
#endif
#ifdef _TIME_3_
            pProcSpec->funcMCUColorConvert((const Ipp16s **)ppMCUBuf, pTempImageData, nTempImageStep);
#endif
#ifdef _GET_M_P
			gettimeofday(&tv_cc1,&tz);
			tv_cc.tv_sec += tv_cc1.tv_sec - tv_cc0.tv_sec;
			tv_cc.tv_usec += tv_cc1.tv_usec - tv_cc0.tv_usec;
#endif
            pProcSpec->funcCopyROI(pTempImageData, nTempImageStep, pImageCur, nImageStep, dstSize);

        }//if (0<nMCUPadWidth)
        pImageData+=nImageMCUVOffset;
    } //for (y=nVMCUPos; y<nVMCUCount; y++)

    if (0<nMCUPadHeight) {
        dstSize.width=nMCUWidth;
        dstSize.height=nMCUPadHeight;
        for (x=nHMCUPos; x<nHMCUCount; x++) {

#ifdef _GET_M_P
			gettimeofday(&tv_vld0,&tz);
#endif			
            pProcSpec->funcHuffmanDecoder( (const Ipp16s **)ppCoeffBuf, pStream, pDecodeState);
			
#ifdef _GET_M_P
			gettimeofday(&tv_vld1,&tz);
			tv_vld.tv_sec += tv_vld1.tv_sec - tv_vld0.tv_sec;
			tv_vld.tv_usec += tv_vld1.tv_usec - tv_vld0.tv_usec;

			gettimeofday(&tv_iqit0,&tz);
#endif			
#ifdef _TIME_2_
            pProcSpec->funcMCUTransform((const Ipp16s **)ppCoeffBuf, ppMCUBuf,
                                        (const Ipp32s **)(pDecodeState->paIdxDCTDequantTable));
#endif
	
#ifdef _GET_M_P
			gettimeofday(&tv_iqit1,&tz);
			tv_iqit.tv_sec += tv_iqit1.tv_sec - tv_iqit0.tv_sec;
			tv_iqit.tv_usec += tv_iqit1.tv_usec - tv_iqit0.tv_usec;

			gettimeofday(&tv_cc0,&tz);
#endif

#ifdef _TIME_3_
            pProcSpec->funcMCUColorConvert((const Ipp16s **)ppMCUBuf, pTempImageData, nTempImageStep);
#endif

#ifdef _GET_M_P
			gettimeofday(&tv_cc1,&tz);
			tv_cc.tv_sec += tv_cc1.tv_sec - tv_cc0.tv_sec;
			tv_cc.tv_usec += tv_cc1.tv_usec - tv_cc0.tv_usec;
#endif			
            pProcSpec->funcCopyROI(pTempImageData, nTempImageStep, pImageData, nImageStep, dstSize);

            pImageData+=nImageMCUHOffset;
        } //for (x=nHMCUPos; x<nHMCUCount; x++)
        if (0<nMCUPadWidth) {
            dstSize.width=nMCUPadWidth;
			
#ifdef _GET_M_P
			gettimeofday(&tv_vld0,&tz);
#endif			
            pProcSpec->funcHuffmanDecoder((const Ipp16s **)ppCoeffBuf, pStream, pDecodeState);
			
#ifdef _GET_M_P
			gettimeofday(&tv_vld1,&tz);
			tv_vld.tv_sec += tv_vld1.tv_sec - tv_vld0.tv_sec;
			tv_vld.tv_usec += tv_vld1.tv_usec - tv_vld0.tv_usec;

			gettimeofday(&tv_iqit0,&tz);
#endif			
#ifdef _TIME_2_
            pProcSpec->funcMCUTransform((const Ipp16s **)ppCoeffBuf, ppMCUBuf,
                                        (const Ipp32s **)(pDecodeState->paIdxDCTDequantTable));
#endif

#ifdef _GET_M_P
			gettimeofday(&tv_iqit1,&tz);
			tv_iqit.tv_sec += tv_iqit1.tv_sec - tv_iqit0.tv_sec;
			tv_iqit.tv_usec += tv_iqit1.tv_usec - tv_iqit0.tv_usec;

			gettimeofday(&tv_cc0,&tz);
#endif            
#ifdef _TIME_3_
			pProcSpec->funcMCUColorConvert((const Ipp16s **)ppMCUBuf, pTempImageData, nTempImageStep);
#endif

#ifdef _GET_M_P
			gettimeofday(&tv_cc1,&tz);
			tv_cc.tv_sec += tv_cc1.tv_sec - tv_cc0.tv_sec;
			tv_cc.tv_usec += tv_cc1.tv_usec - tv_cc0.tv_usec;
#endif

            pProcSpec->funcCopyROI(pTempImageData, nTempImageStep, pImageData, nImageStep, dstSize);
        } //if (0<nMCUPadWidth)
    } //if (0<nMCUPadHeight)

			gettimeofday(&tv_vld1,&tz);
			tv_vld.tv_sec += tv_vld1.tv_sec - tv_vld0.tv_sec;
			tv_vld.tv_usec += tv_vld1.tv_usec - tv_vld0.tv_usec;

    //pDecodeState->nHMCUCurPos=x;
    //pDecodeState->nVMCUCurPos=y;
    nProcessedBitsLen=pStream->nUsedBufBitsLen-nProcessedBitsLen;
    pDecodeState->counter.nProcessedBitsLen+=nProcessedBitsLen;

    return IJX_RTCODE_OK;
}
/* EOF */
